package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.CityAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.CityBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.weidget.SideBar;

import org.json.JSONArray;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class CitySelectActivity extends BaseActivity implements TextWatcher, SideBar.OnTouchingLetterChangedListener {
    private static final String TAG = "CitySelectActivity : ";
    private EditText citySearch;
    private TextView showCurrent, nowLoc;
    private SideBar cityNav;
    private RecyclerView cityListRv;
    private List<CityBean> cityList;
    private CityAdapter cityAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_city_select);
        cityAdapter = new CityAdapter(CitySelectActivity.this, cityList);
        ((TextView) findViewById(R.id.common_title_text)).setText("城市选择");
        findViewById(R.id.common_home_back).setVisibility(View.GONE);
        nowLoc = (TextView) findViewById(R.id.now_loc);
        citySearch = (EditText) findViewById(R.id.city_search);
        citySearch.setSingleLine();
        showCurrent = (TextView) findViewById(R.id.show_current);
        cityNav = (SideBar) findViewById(R.id.city_nav);
        cityNav.setTextView(showCurrent);
        cityNav.setVisibility(View.INVISIBLE);
        cityListRv = (RecyclerView) findViewById(R.id.city_list_rv);
        cityListRv.setLayoutManager(new LinearLayoutManager(CitySelectActivity.this, LinearLayoutManager.VERTICAL, false));
        cityListRv.setItemAnimator(new DefaultItemAnimator());
        cityListRv.setAdapter(cityAdapter);
        cityAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                cityList = cityAdapter.getList();
                Intent intent = new Intent();
                intent.putExtra("city_name", cityList.get(position).getArea_name());
                intent.putExtra("city_id", cityList.get(position).getArea_id());
                setResult(RESULT_OK, intent);
                ActivityCollector.removeActivity(CitySelectActivity.this);
            }

            @Override
            public void onItemLongClickListener(View view, int position) {

            }
        });

    }

    @Override
    protected void initData() {
        nowLoc.setText(SharedPreferencesUtil.getStringPreference(CitySelectActivity.this, "PREFERENCE", "locate_city", ""));
        LogUtils.e(TAG + "nowLoc = " + SharedPreferencesUtil.getStringPreference(CitySelectActivity.this, "PREFERENCE", "locate_city", ""));
    }

    @Override
    protected void loadNetData() {

        if(BaseApplication.cityList != null && !BaseApplication.cityList.isEmpty()){
            cityList = BaseApplication.cityList;
            cityAdapter.setData(BaseApplication.cityList);
            cityNav.setVisibility(View.VISIBLE);
        }else{
            HashMap<String, String> param = new HashMap<>();
            param.put("service", "Public_City.getlist");
            CommonRequestNetData.getJsonArrayData(CitySelectActivity.this, param,true, new CommonRequestNetData.JsonArrayCallBack() {
                @Override
                public void parseJsonArray(JSONArray jsonArray) {
                    LogUtils.e(TAG + "jsArr = " + jsonArray);
                    cityList = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<CityBean>>() {
                    }.getType());
                    Collections.sort(cityList);
                    cityAdapter.setData(cityList);
                    LogUtils.e("cityList aaa = " + cityList);
                    if(cityList != null && !cityList.isEmpty()){
                        cityNav.setVisibility(View.VISIBLE);
                    }
                }
            });
        }
    }


    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        citySearch.addTextChangedListener(this);
        cityNav.setOnTouchingLetterChangedListener(CitySelectActivity.this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        cityAdapter.getFilter().filter(charSequence);


    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void onTouchingLetterChanged(String index) {
        LogUtils.e("cityList = " + cityList);
        if(cityList == null || cityList.isEmpty()){
            return;
        }
        for (int i = 0; i < cityList.size(); i++) {
            if (cityList.get(i).getArea_shou().equals(index)) {
                cityListRv.getLayoutManager().scrollToPosition(i);
                return;
            }

        }
    }
}
